package com.alit.face.koala.controller.api;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alit.face.koala.app.data.AppResponse;
import com.alit.face.koala.mq.apacheapollo.MQTTSender;
import com.alit.face.koala.service.FaceKoalaService;
import com.alit.face.koala.service.api.V2Service;
import com.alit.face.koala.service.face.baidu.BaiduFaceService;
import com.alit.face.koala.websocket.NotifyWebSocket;
import com.litong.utils.request.RequestUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author litong
 * @date 2019年5月10日_上午11:52:02
 * @version 1.0
 */
@RestController
@RequestMapping("api")
@Slf4j
public class APIController {
  @Autowired
  private FaceKoalaService ks;
  @Autowired
  private V2Service v2Service;
  @Autowired
  private NotifyWebSocket notifyWebSocket;
  @Autowired
  private FaceKoalaService faceKalaService;
  @Autowired
  private BaiduFaceService baiduFaceService;

  @RequestMapping("mq/send")
  @ResponseBody()
  public String send(String topic, String message) {
    try {
      boolean send = MQTTSender.send(topic, message);
      if (send) {
        return "success";
      } else {
        return "faslse";
      }
    } catch (Exception e) {
      e.printStackTrace();
      return e.getMessage();
    }
  }

  @RequestMapping("ws/notify/send")
  @ResponseBody
  public String sendWsNotify(String topic, String message) {
    log.info("测试发送websocket消息:" + topic + "," + message);
    boolean sendText = notifyWebSocket.sendText(topic, message);
    if (sendText) {
      return "success";
    } else {
      return "fail";
    }
  }

  @RequestMapping("ws/notify/list")
  public List<String> notifyList() {
    return notifyWebSocket.getIds();
  }

  @RequestMapping("rabbitmq/send")
  public String rabbitmqSend(String message) {
    // sendder.sendMsg(message);
    return "success";
  }

  @RequestMapping("baidu/face/match")
  public String baiduFace(MultipartFile file1, MultipartFile file2) {
    return baiduFaceService.match(file1, file2);
  }

  @RequestMapping("ip")
  public String ip(HttpServletRequest request) {
    String remoteAddr = request.getRemoteAddr();
    Map<String, String> map = new HashMap<>();
    String xRealIP = request.getHeader("X-Real-IP");
    String xForwardedFor = request.getHeader("X-Forwarded-For");
    String ip = RequestUtil.getRemoteAddr(request);
    map.put("remoteAddr", remoteAddr);
    map.put("X-Real-IP", xRealIP);
    map.put("X-Forwarded-For", xForwardedFor);
    map.put("ip", ip);
    String jsonString = JSON.toJSONString(map);
    return jsonString;
  }

  @RequestMapping("koala/login")
  public String login(String ip, String user, String pswd) {
    String koalaBaseUrl = "http://" + ip;
    String retval = null;
    try {
      int authLogin = ks.authLogin(koalaBaseUrl, user, pswd, true);
      retval = authLogin + "";
    } catch (Exception e) {
      return e.getMessage();
    }
    return retval;
  }

  @RequestMapping("koala/photo")
  public String photo(String koalaBaseUrl, String username, String password, MultipartFile photo) {
    byte[] bytes = null;
    try {
      bytes = photo.getBytes();
    } catch (IOException e) {
      e.printStackTrace();
    }
    log.info(koalaBaseUrl + "," + username + "," + password + "," + +bytes.length);
    JSONObject jsonObject = faceKalaService.subjectPhoto(koalaBaseUrl, username, password, bytes);
    return jsonObject.toJSONString();
  }
  // faceKalaService.sbujectPhoto(koalaBaseUrl, username, password,
  // file.getBytes());

  @RequestMapping("koala/subject")
  public String getStaffNameByID(String ip, String user, String pswd, int id) {
    String koalaBaseUrl = "http://" + ip;
    String retval = null;
    try {
      Map<String, String> staffNameByID = ks.getStaffNameByID(koalaBaseUrl, user, pswd, id);
      retval = JSON.toJSONString(staffNameByID);
    } catch (Exception e) {
      return e.getMessage();
    }
    return retval;
  }

  @RequestMapping(value = "v2/user", produces = MediaType.APPLICATION_JSON_VALUE)
  public AppResponse user() {
    return v2Service.user();
  }

  @RequestMapping(value = "v2/system/setting/i18n", produces = MediaType.APPLICATION_JSON_VALUE)
  public AppResponse i18n() {
    return v2Service.i18n();
  }

  @RequestMapping(value = "v2/system/online/lang", produces = MediaType.APPLICATION_JSON_VALUE)
  public AppResponse lang() {
    return v2Service.lang();
  }

  @RequestMapping(value = "v2/system/menu", produces = MediaType.APPLICATION_JSON_VALUE)
  public AppResponse menu() {
    return v2Service.menu();
  }
}
